package net.zjitc.demosecurity6.pojo;


import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author zhao
 * @DATE 2025/4/25 11:03
 */
@Data
public class LoginUser implements UserDetails {

    //用户信息
    private User user;
    //权限信息
    private List<String> perms;

    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null){
            return authorities;
        }
        authorities = new ArrayList<>();
        for (String perm : perms){
            authorities.add(new SimpleGrantedAuthority(perm));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    public LoginUser() {
    }

    public LoginUser(User user, List<String> perms) {
        this.user = user;
        this.perms = perms;
    }
}
